//
//  UseTimerViewController.m
//  UseTimer
//
//  Created by  LXC on 11-8-14.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UseTimerViewController.h"//sdf
@implementation UseTimerViewController
int tag=0,buttonstate=0,invalidatestate=0;
NSString *timetemp;
-(IBAction) startbuttonPressed:(id) sender
{

	if ([secondvalue.text isEqualToString:@""])
	{
		[self alertSimpleAction];
	}
	else 
	{
		if (tag==0) 
		{
			if(buttonstate==0)
			{
				TimeNum=0;
				temp=secondvalue.text;
				TimeNum=[temp intValue];
				TimeNum*=1000;
				buttonstate=1;
			}
			[secondvalue resignFirstResponder];
			timer = [NSTimer scheduledTimerWithTimeInterval:(0.001)target:self selector:@selector(CountDown) userInfo:nil repeats:YES];
			[start setTitle:@"暂停"  forState:UIControlStateNormal];
			invalidatestate=0;
			tag=1;
		}
		else 
		{
			if (invalidatestate==0) 
			{
				[timer invalidate];
				invalidatestate=1;
			}
			
			[start setTitle:@"开始"  forState:UIControlStateNormal];
			tag=0;
		}
	}
}

-(IBAction) stopbuttonPressed:(id) sender
{
	if (invalidatestate==0) 
	{
		[timer invalidate];
		invalidatestate=1;
	}
	[start setTitle:@"开始"  forState:UIControlStateNormal];
	 timetemp= [[NSString alloc]initWithFormat:@"%02d:%02d:%02d:%03d",0,0,0,0];
	disTimeLabel.text=timetemp;
	[timetemp release];
	tag=0;
	buttonstate=0;
	
//	temp=secondvalue.text;
//	TimeNum=[temp intValue];
//	TimeNum*=1000;
}

//现在时间
//-(void)NowTime{
//	NSDate* now = [NSDate date];
//	NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//	NSDateComponents *comps = [[NSDateComponents alloc] init];
//	NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
//	NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
//	comps = [calendar components:unitFlags fromDate:now];
//	hour = [comps hour];
//	min = [comps minute];
//	sec = [comps second]; 
//	disTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
//}

-(void)CountDown{
	
    if (TimeNum == 0) 
	{
        [timer invalidate];
		[start setTitle:@"开始"forState:UIControlStateNormal];
		disTimeLabel.text=@"时间已到";
		tag=0;
		buttonstate=0;
		invalidatestate=1;
    }
	else
	{
		hour=(TimeNum/1000)/3600;
		min=((TimeNum/1000)-(3600*hour))/60;
		sec=(TimeNum/1000)%60;
		mcsec=TimeNum%1000;
		timetemp = [[NSString alloc]initWithFormat:@"%02d:%02d:%02d:%03d",hour,min,sec,mcsec];
		disTimeLabel.text=timetemp;
		[timetemp release];
		TimeNum -= 1;
	}
}

- (void)alertSimpleAction//this is my xiugai
{
	// open an alert with just an OK button
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"输入信息有误！" message:@"当前秒数为空请重新输入!\n"
												   delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
	[alert show];	
	[alert release];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
}


- (void)dealloc {
	[timetemp release];
    [super dealloc];
}

@end
